// TypeScript file
class TrueLoadingUI extends eui.Group
{
    constructor()
    {
        super();
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemovedFromStage, this);

        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddedToStage, this);
    }

    private onRemovedFromStage(evt: egret.Event): void
    {
        if (this.hasEventListener(egret.Event.ENTER_FRAME))
        {
            this.removeEventListener(egret.Event.ENTER_FRAME, this.runLoading, this);
        }
    }

    private runLoading(evt: egret.Event): void
    {
        this.loadingRun.rotation += 3;
    }

    private onAddedToStage(evt: egret.Event): void
    {
        this.setProgress(0, 1);
        this.addEventListener(egret.Event.ENTER_FRAME, this.runLoading, this);
    }

    public setProgress(current: number, total: number): void
    {
        if (this.txProgress)
        {
           
            this.txProgress.text = Math.round(current / total * 100) + "%";
            console.log("TrueLoadingUI:setProgress" + this.txProgress.text);
        }

    }

    public createChildren(): void
    {
        super.createChildren();

        this.loadingRun = new egret.Bitmap(RES.getRes("loading2_png"));
        this.addChild(this.loadingRun);

        this.loadingRun.anchorOffsetX = this.loadingRun.width * .5;
        this.loadingRun.anchorOffsetY = this.loadingRun.height * .5;
        this.loadingRun.x = this.stage.stageWidth * .5;
        this.loadingRun.y = this.stage.stageHeight * .5;

        this.txProgress = new egret.TextField;
        this.txProgress.textAlign = egret.HorizontalAlign.CENTER;
        this.txProgress.verticalAlign = egret.VerticalAlign.MIDDLE;
        this.txProgress.x = this.stage.stageWidth * .5 - 100;
        this.txProgress.width = 200;
        this.txProgress.y = this.stage.stageHeight * .5 - 100;
        this.txProgress.height = 200;
        this.txProgress.size = 30;
        this.txProgress.stroke = 1;
        this.txProgress.strokeColor = 0;
        this.addChild(this.txProgress);
    }
    

    private txProgress: egret.TextField;
    private loadingRun: egret.Bitmap;
}